#include <SoftwareSerial.h>
#define rxPin 6
#define txPin 4
#define TAG_LEN 12
#define NUM_TAGS 8
// set up a new serial
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

byte RFIDcode[TAG_LEN][NUM_TAGS];		 // var that will hold the bytes-in read from the serialBuffer
int RFID_bytes_read = 0;
byte reset_to_ready[6] = {0x05,0x03,0x03,0x26,0x1B,0x61};
byte tagAquiet[14]={0x0D,0x00,0x23,0x02,0xEE,0xE9,0x8A,0x12,0x00,0x01,0x04,0xE0,0xC7,0x3F};
byte tagBquiet[14]={0x0D,0x00,0x23,0x02,0xF1,0x6F,0x8A,0x12,0x00,0x01,0x04,0xE0,0xDD,0x0B};
byte tagCquiet[14]={0x0D,0x00,0x23,0x02,0x85,0xF1,0x8A,0x12,0x00,0x01,0x04,0xE0,0x8F,0x20};
byte tagDquiet[14]={0x0D,0x00,0x23,0x02,0x8D,0xEF,0x8A,0x12,0x00,0x01,0x04,0xE0,0xDB,0x3E};
byte tagEquiet[14]={0x0D,0x00,0x23,0x02,0xC3,0xFE,0x8A,0x12,0x00,0x01,0x04,0xE0,0x3B,0x41};
byte tagFquiet[14]={0x0D,0x00,0x23,0x02,0xFE,0xB4,0x8A,0x12,0x00,0x01,0x04,0xE0,0x3F,0x38};
byte tagGquiet[14]={0x0D,0x00,0x23,0x02,0x48,0xB2,0x8A,0x12,0x00,0x01,0x04,0xE0,0x29,0x5B};
byte tagHquiet[14]={0x0D,0x00,0x23,0x02,0x09,0xD8,0x8a,0x12,0x00,0x01,0x04,0xE0,0xB5,0xF2};
//Tag Identifiers. The last two bytes (CRC) change when in a command
//A) 0000eee98a12000104e0e17c
//B) 0000f16f8a12000104e0fb48
//C) 000085f18a12000104e0a963
//D) 00008def8a12000104e0fd7d
//E) 0000c3fe8a12000104e01d02
//F) 0000feb48a12000104e0197b
//G) 000048b28a12000104e00f18
//H) 000009d88a12000104e093b1

void setup() {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(19200); // set the data rate for the SoftwareSerial port
  Serial.begin(19200); //9600 doesn't work, but 19200 does?
}

void loop() {
  int index, yy, xx;
  RFID_bytes_read = 0;
  mySerial.write(0xfd);
  delay(500);
  for (xx=1; xx<NUM_TAGS+1; xx++){
    mySerial.write(0xf7); //transmitter off
    delay(100);
    mySerial.write(0xf8); //transmitter on
    delay(100);
    mySerial.write(reset_to_ready, 6);
    delay(100);
    for (yy=1; yy<NUM_TAGS+1; yy++) {
      if(xx != yy) {
        Stay_Quiet(yy);
      }
    }
    mySerial.flush();
    delay(100);
    mySerial.write(0xfa);
    delay(100);
    if(mySerial.available());{
      for(index=0; index<TAG_LEN; index++){
        RFIDcode[index][xx] = mySerial.read();
        delay(100);
        Serial.print(RFIDcode[index][xx],HEX);
        RFID_bytes_read++;
      }
      Serial.println();
    }
  }
  mySerial.write(0xfc);
  delay(500);
  Serial.flush();
}


//Function to tell a tag to be quiet
byte Stay_Quiet(int tag)
{
//  "Tell a tag to shut up
  if (tag == 1){
    //Tag A: eee98a12000104e0 \ e17c
    mySerial.write(tagAquiet,14);
  }
  else if(tag == 2){
    //Tag B: f16f8a12000104e0 \ fb48
    mySerial.write(tagBquiet,14);
  }
  else if(tag == 3){
    //Tag C: 85f18a12000104e0 \ a963
    mySerial.write(tagCquiet,14);
  }
  else if(tag == 4){
    //Tag D: 8def8a12000104e0 \ fd7d
    mySerial.write(tagDquiet,14);
  }
  else if(tag == 5){
    //Tag E: c3fe8a12000104e0 \ 1d02
    mySerial.write(tagEquiet,14);
  }
  else if(tag == 6){
    //Tag F: feb48a12000104e0 \ 197b
    mySerial.write(tagFquiet,14);
  }
  else if(tag == 7){
    //Tag G: 48b28a12000104e0 \ 0f18
    mySerial.write(tagGquiet,14);
  }
  else if(tag == 8){
    //Tag H: 09d88a12000104e0 \ 93b1
    mySerial.write(tagHquiet,14);
  }
  else{
    return 0;
  }
  delay(100);
}
